import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.junit.jupiter.api.Test;

import java.util.*;

public class Main {
    @Test
    public void test() {
        List<Map<String,String>> list = new ArrayList<Map<String, String>>();
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("a","a1");
        map.put("b","b1");
        HashMap<String, String> map1 = new HashMap<String, String>();
        map1.put("a","a2");
        map1.put("b","b2");
        list.add(map);
        list.add(map1);

        String s = JSON.toJSONString(list);
        System.err.println(" 序列化list<map>的结果： " + s);
        List<Map> maps = JSON.parseArray(s, Map.class);
        System.err.println(maps);

        Object parse = JSON.parse(s);
        JSONArray ja = (JSONArray)parse;
        List<Map<String,String>> list1 = ja.toJavaObject(List.class);
        list1.stream().forEach(m -> {
            Set<Map.Entry<String, String>> entries = m.entrySet();
            entries.forEach(entry -> {
                System.err.println(entry.getKey() + "," + entry.getValue());
            });
        });
    }



    @Test
    public void testEntity() {
        User user = new User("张三", 18);
        User user1 = new User("李四", 13);
        String jsonString = JSON.toJSONString(user);
        User user2 = JSON.parseObject(jsonString, User.class);
        System.err.println(jsonString);
        System.err.println(user2);

        List<User> list = new ArrayList<>();
        list.add(user);
        list.add(user1);
        String jsonStringList = JSON.toJSONString(list);
        List<User> list1 = JSON.parseArray(jsonStringList, User.class);
        System.err.println(list1);
    }


    @Test
    public void test1() {
        String t = "[{\"age\":18,\"name\":\"张三\"},{\"age\":13,\"name\":\"李四\"}]";
        List<User> list = JSON.parseArray(t, User.class);
        System.err.println(list);
    }


    @Test
    public void test2() {
        String t = "[{\"name\":\"张三\"},{\"name\":\"李四\"}]";
        List<User> list = JSON.parseArray(t, User.class);
        System.err.println(list);
    }
}
